Ext.define('Ext.app.GridPortlet', {
    extend: 'Ext.grid.Panel',
    alias: 'widget.gridportlet',
    uses: [
        'Ext.data.Store'
    ],
    height: 300,

    /**
     * Custom function used for column renderer
     * @param {Object} val
     */
    change: function(val) {
        if (val) {
            return '<span style="color:orange;">' +parseFloat(val).toFixed(2) + '</span>';
        }
        return val;
    },

    change2: function(val) {
        if (val) {
            return '<span style="color:red;">' +parseFloat(val).toFixed(2) + '</span>';
        }
        return val;
    },

    change3: function(val) {
        if (val) {
            return '<span style="color:red;">' + val + '</span>';
        }
        return val;
    },

    /**
     * Custom function used for column renderer
     * @param {Object} val
     */
    pctChange: function(val) {
        if (val > 0) {
            return '<span style="color:green;">' + val + '%</span>';
        } else if (val < 0) {
            return '<span style="color:red;">' + val + '%</span>';
        }
        return val;
    },

    initComponent: function(){
        var store = Ext.create('Ext.data.Store', {
            pageSize: 10000,
            remoteSort: true,
            autoLoad:true,
            fields: [
               {name: 'product_name'},
               {name: 'product_fullname'},
               {name: 'avg_price'},
               {name: 'total_volume'},
               {name: 'total_price'}
            ],
            proxy: {
                type: 'ajax',
                method: 'GET',
                url: '/orders/admin_report?startdate='+new Date().format("yyyy-MM-dd"),
                reader: {
                    type: 'json',
                    root: 'data',
                    totalProperty: 'count'
                },
                simpleSortMode: true,
                listeners:{
                    'exception':function( theproxy, response, operation, options ){
                        var data = eval("("+response.responseText+")");
                        if(!data.success){
                            Ext.Msg.alert('信息提示',data.msg);
                        } else {
                            Ext.Msg.alert("信息提示","系统忙，请稍候再试");
                        }
                    }
                }
            },
            sorters: [
                {
                    property: 'total_price',
                    direction: 'DESC'
                }
            ]
        });

        Ext.apply(this, {
            store: store,
            stripeRows: true,
            columnLines: true,
            loadMask: true,
            features: [{
                ftype: 'summary'
            }],
            columns: [{
                id       :'product_fullname',
                text   : '品名',
                flex: 1,
                sortable : true,
                dataIndex: 'product_fullname'
            },{
                text   : '均价',
                width    : 75,
                sortable : true,
                dataIndex: 'avg_price',
                renderer: this.change
            },{
                text   : '数量',
                width    : 75,
                sortable : true,
                dataIndex: 'total_volume',
                renderer: this.change3,
                summaryType: 'sum',
                summaryRenderer: function (value, summaryData, dataIndex) {
                    return "<span>￥合计：</span>";
                }
            },{
                text   : '小计',
                width    : 75,
                sortable : true,
                dataIndex: 'total_price',
                renderer: this.change2,
                summaryType: 'sum',
                summaryRenderer: function (value, summaryData, dataIndex) {
                    return value.toFixed(2);
                }
            }]
        });

        this.callParent(arguments);
    }
});
